package concurrent.wait_and_notify;

public class UseWaitAndNotify {

    public static void main(String[] args) throws InterruptedException {
        Object o = new Object();
        Thread thread = Thread.currentThread();
        System.out.println("1:"+thread.getName()+"-"+thread.getState());
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                    Thread thread2 = Thread.currentThread();
                    System.out.println("2:"+thread.getName() +"-"+thread.getState());
                    System.out.println("3:"+thread2.getName()+"-"+thread2.getState());
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                //thread1线程想要使用o.wait，必须通过synchronized拿到o的monitor，否则报错
                synchronized (o){
                    o.notify();
                }
            }
        });
        thread1.start();
        //main线程想要使用o.wait，必须通过synchronized拿到o的monitor，否则报错
        synchronized (o){
            o.wait();
            System.out.println("4:"+thread.getName()+"-"+thread.getState());
        }
    }
}
